﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ZK.Common
{
    public class InterfaceExecute
    {
        #region 私有方法
        /// <summary>
        /// 得到正常返回json对象，如果接口服务器返回错误码或本地调用返回错误，错误信息用out ErrorStr返回
        /// </summary>
        /// <param name="ErrorStr">错误信息</param>
        /// <param name="b">本地调用是否异常，如果为ture，表示异常</param>
        /// <param name="ret">如果b=true，则ret为错误信息；否则为json返回字符串</param>
        /// <returns>如果ErrorStr返回空并且返回的Jobjet不为null，则返回的jobjet为正确json对象。</returns>
        public static JObject getJobj(out string ErrorStr, bool b, string ret)
        {
            JObject obj = null;
            ErrorStr = "";
            if (ret == "{}" || ret == "{ }" || ret == "")
            {
                ErrorStr = "error";
                return null;
            }
            else
            {
                if (!b)//本地调用接口成功
                {
                    try
                    {
                        JObject ja = (JObject)JsonConvert.DeserializeObject(ret);
                        //正常返回
                        obj = ja;
                        if (ja != null)
                        {
                            if (!bool.Parse(ja["success"].ToString()) && ja["msg"].ToString().Contains("token"))
                            {
                                ja["codeStatus"] = 4010;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorStr = "解析异常";
                    }
                }
                else //本地调用接口有异常
                {
                    ErrorStr = ret;
                }

                return obj;
            }
        }
        /// <summary>
        /// 处理服务端返回的Error_Code
        /// </summary>
        /// <param name="ja"></param>
        /// <returns></returns>
        public static string doError(JObject ja)
        {
            string ErrorStr;
            string error_code = getString(ja["error_code"]);
            string error = getString(ja["error"]);
            ErrorStr = error;
            return ErrorStr;
        }

        /// <summary>
        /// 得到接口url(url中带参)
        /// </summary>
        /// <param name="method"></param>
        /// <param name="par"></param>
        /// <returns></returns>
        public static string GetUrl(string method, Dictionary<string, string> par)
        {
            string url = "";
            url = GetUrl() + "/" + method;
            if (par != null && par.Keys.Count > 0)
            {
                int i = 0;
                foreach (string key in par.Keys)
                {
                    if (i == 0)
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += key + "=" + par[key];
                    i++;
                }
            }
            return url;
        }

        /// <summary>
        ///  得到接口url(url中不带参数)
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string GetUrl(string method)
        {
            string url = "";
            url = GetUrl() + "/" + method;
            return url;
        }

       /// <summary>
       /// 得到登录接口url（url带参）
       /// </summary>
       /// <param name="method"></param>
       /// <param name="par"></param>
       /// <returns></returns>
        public static string GetLoginUrl(string method, Dictionary<string, string> par)
        {
            string url = "";
            url = GetLoginUrl() + "/" + method;
            if (par != null && par.Keys.Count > 0)
            {
                int i = 0;
                foreach (string key in par.Keys)
                {
                    if (i == 0)
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += key + "=" + par[key];
                    i++;
                }
            }
            return url;
        }

        // 得到接口url
        /// </summary>
        /// <param name="method">接口名称（带路径，从/api开始）</param>
        /// <param name="par">参数</param>
        /// <returns>接口url</returns>
        public static string GetMediaUrl(string method, Dictionary<string, string> par)
        {
            string url = "";
            url = GetMediaUrl() + "/" + method;
            if (par != null && par.Keys.Count > 0)
            {
                int i = 0;
                foreach (string key in par.Keys)
                {
                    if (i == 0)
                    {
                        url += "?";
                    }
                    else
                    {
                        url += "&";
                    }
                    url += key + "=" + par[key];
                    i++;
                }
            }
            return url;
        }

        /// <summary>
        /// 获取webservice域名
        /// </summary>
        /// <returns></returns>
        public static string GetUrl()
        {
            return Settings.GetAppConfig("WebApiUrl");
        }

        public static string GetLoginUrl()
        {
            return Settings.GetAppConfig("ApiLoginUrl");
        }

        public static string GetMediaUrl()
        {
            return Settings.GetAppConfig("MediaUrl");
        }

        public static string getString(object obj)
        {
            return obj == null ? "" : obj.ToString();
        }
        #endregion
    }
}
